package com.erp.module.oem.XxOem

import android.app.Activity
import androidx.appcompat.app.AlertDialog
import android.content.Intent
import android.provider.MediaStore
import android.util.Log
import android.view.View
import android.widget.AdapterView
import com.erp.R
import com.erp.activity.BaseActivity
import com.erp.bean.OemBean
import com.erp.bean.PhotoBean
import com.erp.mclass.UnCeHandler.activity
import com.erp.module.oem.IOemP
import com.erp.module.oem.OemContract
import com.erp.myAdapter.GvphotoAdapter
import com.erp.myAdapter.OemGongduanAdapter
import com.erp.util.Myutil
import kotlinx.android.synthetic.main.activity_oem.*
import kotlinx.android.synthetic.main.header_knoya.*
import java.util.regex.Pattern


class XxOemActivity : BaseActivity(),OemContract.XxOemView, View.OnClickListener {


    lateinit var oembean: OemBean
    lateinit var deletephoto: MutableList<PhotoBean>
    lateinit var adapter: OemGongduanAdapter
    lateinit var IXP: IXxOem
    lateinit var updatedialog: AlertDialog
    lateinit var id: String
    var photoPosition = 0
    override fun getLayoutId(): Int {
        return R.layout.activity_oem
    }

    override fun initCreate() {

        oembean = OemBean()
        deletephoto = ArrayList<PhotoBean>()
        id = intent.getStringExtra("id")
        iv_header_left.visibility = View.VISIBLE
        iv_header_left.setOnClickListener(this)
        tv_header_title.text = "采购详情"
        IXP = IXxOem( this, this, oembean,id)
        adapter = OemGongduanAdapter(oembean.gongxulist,this,this,IXP)

        listview.adapter = adapter
        BtQueding.setOnClickListener(this)
        updatedialog = Myutil.showupdatedialog(this, "正在获取数据", false)
        IXP.getOemPhoto()
    }




    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (data == null) {
            return
        }
        if (resultCode == Activity.RESULT_OK) {
            when (requestCode) {
                99 -> {
                    val selectedImage = data.data
                    val filePathColumns = arrayOf(MediaStore.Images.Media.DATA)
                    val c = selectedImage?.let { contentResolver.query(it, filePathColumns, null, null, null) }
                    c!!.moveToFirst()
                    val columnIndex = c.getColumnIndex(filePathColumns[0])
                    val imagePath = c.getString(columnIndex)
                    addphoto(imagePath)
                }
                100 -> {
                    val name = data.getStringExtra("dir")
                    addphoto(name)
                }
            }
        }
    }

    override fun onClick(v: View?) {
        when(v){
            iv_header_left -> {
                finish()
            }
            BtQueding -> {
                var count = EtBaoshu.text.toString().trim()
                if(count == ""){
                   Myutil.MyTost("完工包数是必须的")
                    return
                }
                updatedialog = Myutil.showupdatedialog(this, "正在确认完工", false)
                IXP.postWangong(EtBaoshu.text.toString())
            }
        }
    }

    private fun addphoto(name: String) {
        val photo = PhotoBean()
        photo.uri = name

        oembean.gongxulist[photoPosition].listphoto.add(photo)
        adapter.notifyDataSetChanged()
    }

    override fun kaishiphoto(position: Int) {
        photoPosition = position
        Log.d("onSuccess","photoPosition="+photoPosition)
    }

    override fun showdialog(title: String) {
        updatedialog = Myutil.showupdatedialog(this, title, false)
    }

    override fun getOemPhotoOk() {
        updatedialog.dismiss()
        Log.d("onSuccess",oembean.toString())
        EtBaoshu.setText(oembean.accomplish_count)
        TvXinxi.text = "单号:"+oembean.o_onum+", 经销商:"+oembean.shop_name
        adapter.notifyDataSetChanged()

    }

    override fun getOemPhotoNo() {
        updatedialog.dismiss()
    }

    override fun postOemPhotoOk() {
        updatedialog.dismiss()
        updatedialog = Myutil.showupdatedialog(this, "正在刷新数据", false)
        IXP.getOemPhoto()
    }

    override fun postOemPhotoNo() {
        updatedialog.dismiss()
    }

    override fun deleteOemPhotoOk() {
        updatedialog.dismiss()
        updatedialog = Myutil.showupdatedialog(this, "正在刷新数据", false)
        IXP.getOemPhoto()
    }

    override fun deleteOemPhotoNo() {
        updatedialog.dismiss()
        updatedialog = Myutil.showupdatedialog(this, "正在刷新数据", false)
        IXP.getOemPhoto()
    }

    override fun postWangongOk() {
        updatedialog.dismiss()
        updatedialog = Myutil.showupdatedialog(this, "正在刷新数据", false)
        IXP.getOemPhoto()
    }

    override fun postWangongNo() {
        updatedialog.dismiss()
    }



}